package suanfa;

import java.util.*;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2024/8/28-上午10:29
 * Since  :   1.0
 * Version:   1.0
 */
public class RandomizedSet {
    List<Integer> list = new ArrayList<>();
    Map<Integer, Integer> map = new HashMap();
    Random random = new Random();

    public RandomizedSet() {

    }

    public boolean insert(int val) {

        boolean flag = map.containsKey(val);
        if (flag)
            return false;

        int index = list.size();
        list.add(val);
        map.put(val, index);
        return true;

    }



    public boolean remove(int val) {
        boolean flag = map.containsKey(val);
         if (!flag)
            return false;
        int index = map.get(val);
        int last=list.get(list.size()-1);
        list.set(index,last);
        map.put(last,index);
        list.remove(list.size()-1);
        map.remove(val);
        return true;

    }

    public int getRandom() {
        int myRandom=random.nextInt(list.size());
        return  list.get(myRandom);
    }
}
